package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareSystemMagessBinding;
import com.sgb.knowledgeshare.entity.MagessListEntity;
import com.sgb.knowledgeshare.view.adapter.MagessAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareSystemMagessModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import java.util.List;

/**
 * Created by：冯齐 on 2020/10/19 15:34
 * Description： 分享-消息-系统通知
 */
public class ShareSystemMagessActivity extends AppActivity {

    ShareSystemMagessModel viewModel;
    private ActivityShareSystemMagessBinding binding;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private boolean isLoadMore;
    private int page;
    private MagessAdapter magessadapter;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ShareSystemMagessActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_system_magess);
        viewModel = new ViewModelProvider(this).get(ShareSystemMagessModel.class);
        viewModel.setActivity(ShareSystemMagessActivity.this);
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("系统通知");
        magessadapter = new MagessAdapter(this,null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(magessadapter);
        binding.lrecyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                page=1;
                viewModel.getSystemMagessList(page);
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                viewModel.getSystemMagessList(page);
            }
        });
      /*  mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                SharePublishActivity.start(ShareSystemMagessActivity.this,"",0,0,0);
            }
        });*/
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    @Override
    protected void init() {
        isLoadMore = false;
        page=1;
        viewModel.getSystemMagessList(page);
    }

    public void setSystemMagessList(List<MagessListEntity.RecordsBean> data) {
        if (data ==null){
            return;
        }

        binding.lrecyclerview.refreshComplete(page);
        binding.lrecyclerview.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.size() > 0) {
                magessadapter.addDatas(data);
            } else {
                MToast.showToast(getApplicationContext(), "没有消息啦");
            }
        } else {
            if (data.size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                magessadapter.setDatas(data);
            } else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                magessadapter.setDatas(null);
            }
        }
    }
}
